import { Close, Right, CircleClose } from '@element-plus/icons-vue'

// 数组操作
// 删除当前
const deleteItem = (list, index) => {
    list.splice(index, 1);
}
// 删除右侧
const deleteRight = (list, index) => {
    list.splice(index + 1, list.length - index - 1);
}
// 全部删除
const deleteAll = (list) => {
    list.splice(0, list.length);
}
// 关闭其他
const deleteOther = (list, index) => {
    const currentItem = list[index]
    list.splice(0, list.length)
    list.push(currentItem)
}

const getContextMenuObj = (list, index, callback) => {
    return {
        menuList: [
            {
                name: '关闭当前',
                key: "current",
                icon: Close
            },
            {
                name: '关闭右侧',
                key: "right",
                icon: Right,
                hidden: () => index == list.length - 1
            },
            {
                name: '关闭其他',
                key: "other",
                icon: CircleClose,
                hidden: () => list.length <= 1
            },
            {
                name: '全部关闭',
                key: "all",
                icon: CircleClose
            }
        ],
        handleClick(event, menuItem) {
            switch (menuItem.key) {
                case 'current':
                    deleteItem(list, index)
                    break;
                case 'right':
                    deleteRight(list, index)
                    break;
                case 'all':
                    deleteAll(list)
                    break;
                case 'other':
                    deleteOther(list, index)
                    break;
                default:
                    break;
            }
            if (callback) {
                // 判断当前active Tab
                callback(index, menuItem)
            }
        }
    }

}

// 判断当前active Tab
const getActiveTab = (list, active, key, clickIndex, menuItem) => {
    // 当前tab还在
    const activeTab = list.filter(item => {
        return item[key] == active
    })[0]
    if (activeTab) {
        return active
    }
    if (menuItem.key == 'current') {
        const nextTab = list[clickIndex + 1]
        if (nextTab) {
            return nextTab[key]
        }
    }

    const lastTab = list[list.length - 1]
    if (lastTab) {
        return lastTab[key]
    }

    return ''

}
export { getContextMenuObj, getActiveTab }